package com.ruibei.ui;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.ruibei.R;
import com.ruibei.models.ListItemModel;
import com.ruibei.ui.fragment.ApprovalDetailFragment;
import com.ruibei.ui.fragment.ApprovalProgressFragment;
import com.ruibei.view.PagerSlidingTabStrip;
import com.ruibei.view.TitleBarView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

public class NewsDetailActivity extends BaseActivity{

	@InjectView(R.id.news_detail_title_bar)
	TitleBarView title_bar_view;
	//tab选项卡控制类
	@InjectView(R.id.tabs)
	PagerSlidingTabStrip mTabs;
	
	@InjectView(R.id.pager)
	ViewPager mPager;
	
	private MyPagerAdapter mAdapter;
	private ListItemModel mItemModel;
	private ApprovalDetailFragment approvalDetailFragment;
	private ApprovalProgressFragment approvalProgressFragment;
	String[] titles = { "内容", "进度"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_detail);
		ButterKnife.inject(this);
		
		initView();
	}
	
	private void initView(){
		mItemModel = (ListItemModel) getIntent().getSerializableExtra(
				"itemModel");
		title_bar_view.setTitleText(mItemModel.getTitle().toString());
		title_bar_view.setBtnLeft(R.drawable.back, R.string.back);
		title_bar_view.setBtnLeftOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		mAdapter = new MyPagerAdapter(getSupportFragmentManager(),titles);
		mPager.setAdapter(mAdapter);
		
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
				.getDisplayMetrics());
		mPager.setPageMargin(pageMargin);
		mTabs.setViewPager(mPager);
	}
	
	public class MyPagerAdapter extends FragmentPagerAdapter {
		
		String[] _titles;
		public MyPagerAdapter(FragmentManager fm,String[] titles) {
			super(fm);
			_titles=titles;
		}
		
		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}
		
		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
				case 0:
					if (approvalDetailFragment == null) {
						approvalDetailFragment = new ApprovalDetailFragment();
					}
					return approvalDetailFragment;
				case 1:
					if (approvalProgressFragment == null) {
						approvalProgressFragment = new ApprovalProgressFragment();
					}
					return approvalProgressFragment;
				default:
					return null;
			}
		}

	}
}
